import os

from google.appengine.ext import db
from google.appengine.ext.webapp import template

import utils
import models
import config


class HelpHelper(object):
    "A helper class for Help"
    def __init__(self):
        self.topics = models.Topic.all().filter('status = ', True)
        self.all_topics = models.Topic.all()
    
    def all_view(self):
        """Yields a list of tuple object containing 
        topic, list of subtopics in topic
        """
        for one_topic in self.topics.filter('is_template = ',True).filter('name !=', 'Root'):
            yield(one_topic, (children for children in one_topic.child))

    def topic_questions(self, k):
        """Returns Questions
        which are marked as child node of a particular topic/subtopic 
        """
        return (children for children in db.get(k).child_questions)
    
    def question_answer(self,k):
        return db.get(k)
    
    def search(self,keyword):
        return utils.search(keyword)
    
    def featured_help(self):
        return utils.featured_questions()
    
    def recent_help(self):
        return utils.recent_help_topic()
    

class TemplateHelper(object):
    def __init__(self, theme = config.THEME ):
        self.template_root = 'template'
        self.theme_root = os.path.join(self.template_root, theme)
    
    def render_output(self, template_file, template_values):
        """Renders the output to a template. The template file 
        must be passed relative to the theme directory
        """ 
        if not template_file.endswith('.html'):
            template_file = template_file + '.html'
        template_path = os.path.join(self.theme_root, template_file)
        return template.render(template_path, template_values)

class ActualView():
    def __init__(self):
        self.help = HelpHelper()
        self.template = TemplateHelper()
    
    def all_help(self):
        template_values = {'all_help': self.help.all_view(),}
        return self.template.render_output('all_help', template_values)
    
    def topic_help(self, k):
        template_values = {'questions':self.help.topic_questions(k),}
        return self.template.render_output('topic_help', template_values)
    
    def question_help(self, k):
        template_values = {'question':self.help.question_answer(k),}
        return self.template.render_output('question_answer', template_values)
    
    def search_help(self, keyword):
        template_values = {'keyword':keyword, 'results':self.help.search(keyword),}
        return self.template.render_output('search', template_values)
    
    def breadcrumbs(self, k = None):
        breadcrumb = []
        if not k:
            return
        go_ahead = True
        while(go_ahead):
            cnode = db.get(k)
            breadcrumb.append(cnode)
            if cnode.head.name == 'Root':
                go_ahead = False
            k = cnode.head.key()
        return breadcrumb
    
    def sidebar(self):
        template_values = {'featured_questions': self.help.featured_help(),
                           'recent_questions':self.help.recent_help(),}
        return self.template.render_output('sidebar', template_values)